package com.baibei.product.trade;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

import com.baibei.basic.BaibeiBasicDialogFragment;
import com.baibei.ebec.quotation.QuotationManager;
import com.baibei.ebec.quotation.event.IQuotationEvent;
import com.baibei.model.QuotationInfo;
import com.baibei.model.TradeParams;
import com.baibei.model.TradeType;
import com.baibei.product.R;
import com.baibei.product.R2;
import com.baibei.quotation.QuotationFilter;
import com.baibei.widget.dialog.BaibeiAlertDialog;
import com.rae.swift.Rx;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 订货确定对话框
 * Created by ChenRui on 2017/8/7 0007 16:31.
 */
public class TradeConfirmFragment extends BaibeiBasicDialogFragment implements IQuotationEvent {
    @BindView(R2.id.tv_dialog_title)
    TextView mDialogTitle;
    @BindView(R2.id.tv_quantity)
    TextView mQuantity;
    @BindView(R2.id.tv_amount)
    TextView mAmount;
    @BindView(R2.id.tv_fee)
    TextView mFee;
    @BindView(R2.id.tv_up)
    TextView mUp;
    @BindView(R2.id.tv_down)
    TextView mDown;
    @BindView(R2.id.tv_use_ticket)
    TextView mTvUseTicket;
    @BindView(R2.id.tv_last_price)
    TextView mTvLastPrice;

    Unbinder unbinder;
    private TradeParams mTradeParams;

    protected boolean mIsEnsure;
    private int productId;
    private double close;

    public static TradeConfirmFragment newInstance(TradeParams param) {
        Bundle args = new Bundle();
        args.putParcelable("param", param);
        TradeConfirmFragment fragment = new TradeConfirmFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTradeParams = getArguments().getParcelable("param");
        QuotationFilter filter = new QuotationFilter(QuotationFilter.ACTION_QUOTATION_PRODUCT);
        QuotationManager.getInstance().register(this,filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        QuotationManager.getInstance().unregister(this);
    }

    @Override
    protected void onBindView(View view) {
        unbinder = ButterKnife.bind(this, view);
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.widget_dialog_trade_confirm;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new BaibeiAlertDialog(getContext());
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mDialogTitle.setText(mTradeParams.type == TradeType.BUY ? getString(R.string.title_trade_buy) : getString(R.string.title_trade_sell));
        mQuantity.setText(getString(R.string.format_trade_quantity, mTradeParams.quantity));
        //定金
        mAmount.setText(getString(R.string.format_trade_rmb, mTradeParams.couponInfo == null ? Rx.formatPrice(mTradeParams.amount) : Rx.formatPrice(0)));
        //订货服务费
        mFee.setText(getString(R.string.format_trade_rmb, mTradeParams.couponInfo == null ? Rx.formatPrice(mTradeParams.fee) : Rx.formatPrice(0)));
        mUp.setText(mTradeParams.upPercent + "%");
        mDown.setText(mTradeParams.downPercent + "%");

        mTvUseTicket.setVisibility(mTradeParams.couponInfo == null ? View.GONE : View.VISIBLE);
        if (mTradeParams.couponInfo != null) {
            mTvUseTicket.setText(String.format(getString(R.string.submit_ticket_count), mTradeParams.couponInfo.getFaceValue()));
        }
        productId = mTradeParams.productId;
        close = mTradeParams.close;
    }

    @OnClick(R2.id.btn_cancel)
    public void onCancelClick() {
        dismiss();
    }

    @OnClick(R2.id.btn_ensure)
    public void onEnsureClick() {
        mIsEnsure = true;
        dismiss();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public boolean isEnsure() {
        return mIsEnsure;
    }

    public void show(android.support.v4.app.FragmentManager manager, TradeParams params) {
        mTradeParams = params;
        getArguments().putParcelable("param", params);
        mIsEnsure = false;
        super.show(manager, "tradeConfirm");
    }

    @Subscribe
    @Override
    public void onEvent(SparseArray<QuotationInfo> quotations) {
        QuotationInfo quotationInfo = quotations.get(productId);
        double last = quotationInfo.getLast();
        mTvLastPrice.setText(last +"");
        if (last > close){
            mTvLastPrice.setTextColor(getResources().getColor(R.color.buyColor));
        } else if (last < close){
            mTvLastPrice.setTextColor(getResources().getColor(R.color.sellColor));
        } else {
            mTvLastPrice.setTextColor(getResources().getColor(R.color.rgb666666));
        }
    }
}
